import numpy as np
import math
import os
import shutil
# 计算精确度
def show_accuracy(y_hat, y_train, type):
    count = 0
    for i in range(y_hat.shape[0]):
        if y_hat[i] == y_train[i]:
            count += 1
    acc = count / y_hat.shape[0]
    print('{}精确度为{}'.format(type, acc))

# 计算同一点集中 两点之间距离，给定两点数值和data
def Euclidean_distance(data, num1, num2):
    x1 = data[2 * num1]
    y1 = data[2 * num1 + 1]
    x2 = data[2 * num2]
    y2 = data[2 * num2 + 1]
    distance = np.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
    return distance

# 计算不同点集中 两点之间距离，给定两点数值和两组data
def Euclidean_distance11(data1, num1, data2, num2):
    x1 = data1[2 * num1]
    y1 = data1[2 * num1 + 1]
    x2 = data2[2 * num2]
    y2 = data2[2 * num2 + 1]
    distance = np.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
    return distance


def Angle(data, num1, num2, num3):
    """
    data：某一组数据
    num1, num2, num3 为三个点序号
    """
    if num1 is None or num2 is None or num3 is None:
        return 0
    else:
        a = Euclidean_distance(data, num2, num3)
        b = Euclidean_distance(data, num1, num3)
        c = Euclidean_distance(data, num1, num2)
        # print('a={},b={},c={}'.format(a,b,c))
        if 2 * a * c != 0:
            return  math.degrees(math.acos((a ** 2 + c ** 2 - b ** 2) / (2 * a * c)))  # 计算出cos弧度，转换为角度
        return 0

def get_dist_feature(x_pose,x_righthand,x_lefthand):
    nose_finger_dist=0
    if x_lefthand[0]!=0:
        nose_finger_dist+=5*Euclidean_distance11(x_pose, 0 ,x_lefthand, 8)
    elif x_righthand[0]!=0:
        nose_finger_dist += 5*Euclidean_distance11(x_pose, 0, x_righthand, 8)
    # print('Euclidean_distance(x_pose, 15, 23)={}'.format(Euclidean_distance(x_pose, 15, 23)))
    # print('5 * Euclidean_distance(x_righthand, 1, 4)={}'.format(5 * Euclidean_distance(x_righthand, 1, 4)))
    # print('x_righthand, 5, 6, 7={}'.format(Angle(x_righthand, 5, 6, 7)/180))
    dist_feature= [
                      # Euclidean_distance(x_pose, 0, 16),
                      # Euclidean_distance(x_pose, 0, 15),
                      Euclidean_distance(x_pose, 15, 16),
                      Euclidean_distance(x_pose, 16, 12),
                      Euclidean_distance(x_pose, 11, 15),#0.05
                      Euclidean_distance(x_pose, 16, 24),#重要0.1
                      Euclidean_distance(x_pose, 15, 23),
                      5 * Euclidean_distance(x_righthand, 1, 4),
                      5 * Euclidean_distance(x_righthand, 12, 8),
                      5 * Euclidean_distance(x_lefthand, 1, 4),
                      5 * Euclidean_distance(x_lefthand, 12, 8),
                      nose_finger_dist,
                      # Angle(x_righthand,  1, 2,3)/180,
                      # Angle(x_righthand, 5, 6, 7)/180 ,
                      # Angle(x_righthand, 9, 10, 11)/180,
                      # Angle(x_lefthand,1,2,3)/180,
                      # Angle(x_lefthand, 5, 6, 7)/180,
                      # Angle(x_lefthand, 9, 10, 11)/180
                      ]
    return dist_feature

#python删除文件的方法 os.remove(path)path指的是文件的绝对路径,如：
# os.remove(r"E:\code\practice\data\1.py")#删除文件
# os.rmdir(r"E:\code\practice\data\2")#删除文件夹（只能删除空文件夹）
# shutil.rmtree(r"E:\code\practice\data\2")#删除文件夹
# path_data = "E:\code\practice\data"#
def del_file(path_data):
    for i in os.listdir(path_data) :# os.listdir(path_data)#返回一个列表，里面是当前目录下面的所有东西的相对路径
        file_data = path_data + "\\" + i#当前文件夹的下面的所有东西的绝对路径
        if os.path.isfile(file_data) == True:#os.path.isfile判断是否为文件,如果是文件,就删除.如果是文件夹.递归给del_file.
            os.remove(file_data)
        else:
            del_file(file_data)

